<?php
/*
This sends a little bit of JSON to say if the user is logged in. 
*/

require_once( 'core.php' );
require_once( 'current_user_api.php' );
require_once( 'string_api.php' );
require_once( 'file_api.php' );
require_once( 'bug_api.php' );
require_once( 'custom_field_api.php' );
require_once( 'collapse_api.php' );
require_once( 'relationship_api.php' );


auth_ensure_user_authenticated( );
//helper_begin_long_process( );

$t_plugin_path = config_get( 'plugin_path' );
require_once( $t_plugin_path . 'DataRefine' . DIRECTORY_SEPARATOR . 'ProjectsMetadata.php' );
require_once( $t_plugin_path . 'DataRefine' . DIRECTORY_SEPARATOR . 'TableMetadata.php' );
require_once( $t_plugin_path . 'DataRefine' . DIRECTORY_SEPARATOR . 'JSONstore.php' );

$authHashPrefix = plugin_config_get( 'authHashPrefix' );
$refineHumanURI = plugin_config_get( 'refineBaseURI' ); //for human linking
$refineURI = plugin_config_get( 'refineLocalURI' );
if(!$refineURI){
	$refineURI = plugin_config_get( 'refineBaseURI' );
}

# extracts the user information for the currently logged in user
# and prefixes it with u_
$f_user_id = gpc_get_int( 'id', auth_get_current_user_id() );
$row = user_get_row( $f_user_id );

extract( $row, EXTR_PREFIX_ALL, 'u' );
$u_email = user_get_email( $u_id );
$u_realname = user_get_realname( $u_id );



//Needed for making an issue
$t_project_id = helper_get_current_project();
	
if($t_project_id != 0){
	$projectsMetadataObj = new ProjectsMetadata;
	$projectsMetadataObj->authHashPrefix = $authHashPrefix;
	$projectsMetadataObj->refineURI = $refineURI;
	$projectsMetadataObj->refineHumanURI = $refineHumanURI;
	$projectsMetadataObj->fallBack_issueProject_id = $t_project_id;
	$projectsMetadataObj->fallBack_issueUserID = auth_get_current_user_id();
	$projectsMetadataObj->fallBack_issueProfile_id = gpc_get_int( 'profile_id', 0 );
	
	$projectsMetadataObj->get_gr_projects();
	$projectsMetadataObj->get_only_projectsdata();
	
	$projectIDkey = $_POST["grProject"]; 
	$curGoogleRefineProjects = $projectsMetadataObj->curGoogleRefineProjects;
	$project = $curGoogleRefineProjects["projects"][$projectIDkey]; //gets the current project data from Google Refine
	
	$projectsMetadata = $projectsMetadataObj->projectsMetadata;
	if(!is_array($projectsMetadata)){
		$projectsMetadata = array();
	}
	
	$projectsMetadata = $projectsMetadataObj->accession_new_gr_project($projectIDkey, $project,  $projectsMetadata);
	$projectsMetadataObj->projectsMetadata = $projectsMetadata;
	$projectsMetadataObj->save_projects_metadata();
	
	$output = json_encode(array("grProject" => $projectIDkey, "grData" => $project, "success" => true), true);
}
else{
	$output = json_encode(array("grProject" => $projectIDkey, "grData" => $project, "success" => false, "noCurProject" => true), true);
}

//now output for AJAX call from the Refine Login Screen
if(!isset($_GET["callback"])){
	header('Content-Type: application/json; charset=utf8');
	echo $output;
}
else{
	header('Content-Type: application/json; charset=utf8');
	$callback = $_GET["callback"];
	echo $callback."(".$output.");";
}


?>
